package fr.kyvos.optimization;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public abstract class AlgoTestThread extends Thread {
	private FileOutputStream output;
	protected String nom_fichier;
	
	protected KnapSackProblem problem;
	
	private int nb_combinaisons;
	
	protected int type_solution_init;
	protected int[] nb_iterations_range;
	protected int nb_iteration_range_pas;
	
	public AlgoTestThread(KnapSackProblem problem){
		nom_fichier = "";
		
		this.problem = problem;
		
		nb_combinaisons = 0;
		
		type_solution_init = KspSolution.SOLUTION_INITIALE_ALEA;
		nb_iterations_range = new int[]{};
		nb_iteration_range_pas = 10;
	}
	
	public void change_filename(String filename){
		nom_fichier = filename;
	}
	
	public void change_config(int nb_combinaisons, int type_solution_init, int[] nb_iterations_range, int nb_iteration_range_pas){
		this.nb_combinaisons = nb_combinaisons;
		this.type_solution_init = type_solution_init;
		this.nb_iterations_range = nb_iterations_range;
		this.nb_iteration_range_pas = nb_iteration_range_pas;
	}
	
	protected void write_file_header() throws IOException {
		write_in_file("combinaison;");
		write_in_file("nombre d'iterations;");
		write_in_file("meilleur fitness");
	}
	
	public void write_in_file(String text) throws IOException {
		output.write(text.getBytes());
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
		try {
			
		output = new FileOutputStream(new File(nom_fichier),false);
		write_file_header();
		
		for (int i=0; i<nb_combinaisons; i++) {
			for (int j=nb_iterations_range[0]; j<=nb_iterations_range[1]; j=j+nb_iteration_range_pas) {
				test(i, j);
			}
		}
		
		output.close();
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Realise un test pour une combinaison de parametres (identifiee par son indice dans les tableaux) et un certain nombre d'iterations
	 */
	protected abstract void test(int no_combinaison, int nb_iterations);
}
